package design.simdi.code.final_paper.utils;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.wizard.IWizard;
import org.apache.wicket.extensions.wizard.IWizardModel;
import org.apache.wicket.extensions.wizard.IWizardStep;
import org.apache.wicket.markup.html.form.Form;

import design.simdi.code.final_paper.site.panels.MyWizard;

/**
 * 
 * @author Marius Dumitriu - (Jun 21, 2010 - 11:31:39 PM)
 * 
 */
public class AjaxFinishButton extends WizardAjaxButton {

	private static final long serialVersionUID = 1L;

	public AjaxFinishButton(String id, IWizard wizard, Form<?> form) {
		super(id, wizard, form, "org.apache.wicket.extensions.wizard.finish");

	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onClick(AjaxRequestTarget target, Form form) {
		IWizardModel wizardModel = getWizardModel();
		IWizardStep step = wizardModel.getActiveStep();

		// let the step apply any state
		step.applyState();

		// if the step completed after applying the state, notify the wizard
		if (step.isComplete()) {
			getWizardModel().finish();
			onFinish(target);
		} else {
			error(getLocalizer().getString("org.apache.wicket.extensions.wizard.FinishButton.step.did.not.complete",
					this));
		}

	}

	protected void onError(final AjaxRequestTarget target, Form<?> form) {

		target.addComponent(findParent(MyWizard.class));

	}

	public final boolean isEnabled() {
		IWizardStep activeStep = getWizardModel().getActiveStep();
		return (activeStep != null && getWizardModel().isLastStep(activeStep));

	}

	public void onFinish(AjaxRequestTarget target) {

	}

}
